#include <hw/interrupts.h>
#include <hw/rtc.h>
#include <utils/io.h>
#include <time/time.h>

// 1 tick per 1 millisecond
#define FREQUENCY 1000

void time_update();

static void timer_handler(struct interrupt_info info) {
    time_update();
}

void timer_set_freq(uint32 freq) {
    uint32 divisor = 1193180 / freq;
    outb(0x43, 0x36);
    outb(0x40, divisor & 0xFF);
    outb(0x40, divisor >> 8);
}

void timer_init() {
    timer_set_freq(FREQUENCY);
    register_irq(0, &timer_handler);
}
